package com.acme.rogelio;

import com.acme.entity.Contacto;
import com.acme.entity.Empleado;
import com.acme.entity.util.DBManager;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jdesktop.beansbinding.Binding;

public class PanelEmpleado extends JPanel implements ChangeListener {

    /** Creates new form Empleado */
    public PanelEmpleado() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        empleado1 = new com.acme.entity.Empleado();
        queryEmpleado = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT e FROM Empleado e");
        listaEmpleado = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : queryEmpleado.getResultList();
        entityManager1 = DBManager.getEntityManager();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaEmpleado = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        subPanelEmpleado1 = new com.acme.rogelio.SubPanelEmpleado();

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaEmpleado, TablaEmpleado, "TablaEmpleado");
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${numero}"));
        columnBinding.setColumnName("Numero");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${contacto}"));
        columnBinding.setColumnName("Contacto");
        columnBinding.setColumnClass(com.acme.entity.Contacto.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, TablaEmpleado, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.contacto}"), TablaEmpleado, org.jdesktop.beansbinding.BeanProperty.create("selectedElement"));
        bindingGroup.addBinding(binding);

        TablaEmpleado.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                TablaEmpleadoMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(TablaEmpleado);
        TablaEmpleado.getColumnModel().getColumn(0).setMinWidth(100);
        TablaEmpleado.getColumnModel().getColumn(0).setPreferredWidth(100);
        TablaEmpleado.getColumnModel().getColumn(0).setMaxWidth(100);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jButton1.setText("AGREGAR");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);
        jToolBar1.add(jSeparator1);

        jButton2.setText("ELIMINAR");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);
        jToolBar1.add(jSeparator2);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, TablaEmpleado, org.jdesktop.beansbinding.ELProperty.create("${selectedElement}"), subPanelEmpleado1, org.jdesktop.beansbinding.BeanProperty.create("bean"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE)
            .addComponent(subPanelEmpleado1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(subPanelEmpleado1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Empleado nuevo= new Empleado ();
        nuevo.setContacto( new Contacto());
         nuevo.addChangeListener(this);
         bindingGroup.unbind();
         listaEmpleado.add(nuevo);
         bindingGroup.bind();
         int fila = listaEmpleado.size()-1;
         TablaEmpleado.setRowSelectionInterval(fila, fila);
         subPanelEmpleado1.setEnabled(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int fila = TablaEmpleado.getSelectedRow();
        if (fila != -1) {
            String mesg = "¿Estas loko si quieres eliminar..pero bueno %s?";
            Empleado actual = listaEmpleado.get(fila);
            int resp = JOptionPane.showConfirmDialog(this, String.format(mesg,actual), "CONFIRMACION", JOptionPane.YES_NO_OPTION);
            if (resp == JOptionPane.YES_OPTION) {
                Binding bin = bindingGroup.getBinding("tablaEmpleado");
                bin.unbind();
                listaEmpleado.remove (actual);
                subPanelEmpleado1.setEnabled(false);
                subPanelEmpleado1.setBean(new Empleado());
                bin.bind();

            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void TablaEmpleadoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaEmpleadoMouseReleased
       int fila = TablaEmpleado.getSelectedRow();
       if (fila != -1){ 
           subPanelEmpleado1.setEnabled(true);
           subPanelEmpleado1.activarCampoNuemro(false);
       }
    }//GEN-LAST:event_TablaEmpleadoMouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TablaEmpleado;
    private com.acme.entity.Empleado empleado1;
    private javax.persistence.EntityManager entityManager1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private java.util.List<Empleado> listaEmpleado;
    private javax.persistence.Query queryEmpleado;
    private com.acme.rogelio.SubPanelEmpleado subPanelEmpleado1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    @Override
    public void stateChanged(ChangeEvent e) {
       Binding bin = bindingGroup.getBinding("TablaEmpleado");
       bin.unbind();
       bin.bind();
    }

}
